/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import systole.domain.clinicalInformation.Sport;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.SportBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 *
 */
public class SportBrokerDB extends BrokerDB implements SportBroker {

    /**
     */
    public SportBrokerDB() {
        super();
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.SportBroker#getAllSports()
     */
    @Override
    public List<Sport> getAllSports() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting all sports");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<Sport> list = currentSession.createQuery("from Sport s order by upper(s.name)").list();
            this.logger.logDebug("getting sports successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all sports, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las actividades físicas", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.SportBroker#insert(systole.domain.clinicalInformation.Sport)
     */
    @Override
    public void insert(Sport sport) throws ExceptionDAO {
        try {
            this.logger.logDebug("save sport ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(sport);
            this.logger.logDebug("save successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save sport, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.SportBroker#update(systole.domain.clinicalInformation.Sport)
     */
    @Override
    public void update(Sport sport) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating sport ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(sport);
            this.logger.logDebug("update successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on update sport, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    @SuppressWarnings("unchecked")
    public boolean existSport(String name, Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist Sport");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Sport.class).
                    add((id == null) ? Restrictions.eq("name", name)
                    : Restrictions.and(Restrictions.eq("name", name), Restrictions.ne("id", id)));
            List list = criteria.list();
            this.logger.logDebug("exist Sport successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist Sport, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe la actividad física", e.fillInStackTrace());
        }
    }

    public Sport getSportByName(String name) throws ExceptionDAO {
        try {
            this.logger.logDebug("get Sport by name");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Sport.class).
                    add(Restrictions.eq("name", name)).setMaxResults(1);
            return (Sport) criteria.uniqueResult();
        } catch (HibernateException e) {
            this.logger.logError("error on get Sport by name, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener la actividad física", e.fillInStackTrace());
        }
    }
}
